//
//  FBtManager.m
//  JwPack
//
//  Created by 陈警卫 on 2025/7/24.
//

#import "FBtManager.h"

#define kFBtServiceUUID @"00FF"
#define kFBtWriteCharaUUID @"FF01"
#define kFBtNotifCharaUUID @"FF01"

@interface FBtManager ()

@property (nonatomic, strong) FBtCorer *btCorer;
@property (nonatomic, strong) FBtSession *session;

@end

@implementation FBtManager

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupData];
    }
    return self;
}

- (void)setupData {
    
    kJwWeak(self);
    
    // FBtCorer
    
    FBtConfig *config = [[FBtConfig alloc] init];
    self.btCorer = [[FBtCorer alloc] initWithConfig:config];
    
    self.btCorer.didState = ^(FBtState state) {
        [wself btStateAction:state];
    };
    self.btCorer.didScanedPeri = ^(FBtPeriModel * _Nonnull periModel) {
        [wself btScanedPeriAction:periModel];
    };
    self.btCorer.didNotifedPeri = ^(FBtPeriModel * _Nonnull periModel) {
        [wself btNotifedPeriAction:periModel];
    };
    self.btCorer.didNotifedValue = ^(FBtPeriModel * _Nonnull periModel, NSData * _Nonnull data) {
        [wself.session receiveData:data periModel:periModel];
    };
    self.btCorer.didDisconncetPeri = ^(FBtPeriModel * _Nonnull periModel) {
        [wself btDisconncetPeriAction:periModel];
    };
    self.btCorer.didConncetFailPeri = ^(FBtPeriModel * _Nonnull periModel) {
        [wself btConncetFailPeriAction:periModel];
    };
    
    // FBtSession
    self.session = [[FBtSession alloc] init];
    
}

- (void)scanConnect {
    FBtPeriModel *periModel1 = [[FBtPeriModel alloc] init];
    periModel1.connectName = @"MI_60M1601LGYYY001";
    periModel1.serviceUUIDs = @[[CBUUID UUIDWithString:@"00FF"]];
    periModel1.notifUUIDs = @[[CBUUID UUIDWithString:@"FF01"]];
    periModel1.writeUUIDs = @[[CBUUID UUIDWithString:@"FF01"]];
    [self.btCorer addScanConnectPeriModel:periModel1];
    
    FBtPeriModel *periModel2 = [[FBtPeriModel alloc] init];
    periModel2.connectName = @"MI_60M4102LGYYY001";
    periModel2.serviceUUIDs = @[[CBUUID UUIDWithString:@"00FF"]];
    periModel2.notifUUIDs = @[[CBUUID UUIDWithString:@"FF01"]];
    periModel2.writeUUIDs = @[[CBUUID UUIDWithString:@"FF01"]];
    [self.btCorer addScanConnectPeriModel:periModel2];
    
    [self.btCorer scan];
}

- (void)btStateAction:(FBtState)state {
    //kFBtLog(@"btStateAction--%ld", state);
}

- (void)btScanedPeriAction:(FBtPeriModel *)periModel {
    //kFBtLog(@"btScanedPeriAction--%@", periModel);
}

- (void)btNotifedPeriAction:(FBtPeriModel *)periModel {
    self.session.periModels = [self.btCorer notifedPeriModel];
}

- (void)btDisconncetPeriAction:(FBtPeriModel *)periModel {
    self.session.periModels = [self.btCorer notifedPeriModel];
}

- (void)btConncetFailPeriAction:(FBtPeriModel *)periModel {
    self.session.periModels = [self.btCorer notifedPeriModel];
}

#pragma mark - func

- (void)setNet {
    //FBtMessageModel *messageModel = [FBtMessageModel model_setNetWithSsid:@"FOX-ESS-OFFICE" pwd:@"mtmt9999"];
    FBtMessageModel *messageModel = [FBtMessageModel model_setNetMSWithSsid:@"FOX-ESS-OFFICE" pwd:@"mtmt9999"];
    messageModel.periModel = self.btCorer.periModels[0];
    [self.session pushWithModel:messageModel];
    messageModel.didReceiveAction = ^(FBtMessageModel *messageModel) {
        NSLog(@"%@", messageModel);
        [self.btCorer scan];
    };
    
}

/// 发送数据
- (void)pushMessageModel:(FBtMessageModel *)messageModel {
    [self.session pushWithModel:messageModel];
}



@end
